﻿using Group3.MediaTool.Data.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Group3.MediaTool.Provider
{
    public class ObjectTypeProvider
    {
        public Type getClassTypeFromString(String classType)
        {
            CompositionContainer container;
            DirectoryCatalog drcat;
            
            string dirname = Path.Combine(StaticConfig.pathDataDefinition, "Classes");

            if (!Directory.Exists(dirname))
                Directory.CreateDirectory(dirname);
            drcat = new DirectoryCatalog(dirname);
            //Create the CompositionContainer with the parts in the catalog
            container = new CompositionContainer(drcat);
            string formatedClassName = (classType.Substring(0, 2) != "E_" ? "E_" + classType : classType);

            Type[] types = null;

            if (container != null)
            {
                var currentLoaded = container.GetExports<Group3.MediaTool.Interface.IObjectRelationModel>();
                if (currentLoaded != null && currentLoaded.Count() > 0)
                {
                    var currentExport = currentLoaded
                        .Where(x => x.Value.GetType().Name.ToUpper() == formatedClassName.ToUpper())
                        .Select(x => x);
                    try
                    {
                        String dllFilename = Path.Combine(drcat.FullPath, formatedClassName) + ".dll";
                        if (File.Exists(dllFilename))
                        {
                            types = Assembly.LoadFile(dllFilename).GetTypes();
                        }
                        else
                        {
                            DataDefinitionIO ddIO = new DataDefinitionIO();
                            ddIO.writeLogFile("DLL for " + formatedClassName + " not found " + dllFilename, "WARNING");
                        }
                    }
                    catch (Exception e)
                    {
                        DataDefinitionIO ddIO = new DataDefinitionIO();
                        ddIO.writeLogFile(e.ToString(), "ERROR");
                    }
                }
            }

            Type type = null;
            if (types != null)
            {
                type = types.First();//types.First(t => t.Name.ToUpper() == classType);
            }
            return type;
        }

        public List<String> getAllClassTypes()
        {
            CompositionContainer container;
            DirectoryCatalog drcat;

            string dirname = Path.Combine(StaticConfig.pathDataDefinition, "Classes");
            if (!Directory.Exists(dirname))
                Directory.CreateDirectory(dirname);
            drcat = new DirectoryCatalog(dirname);
            //Create the CompositionContainer with the parts in the catalog
            container = new CompositionContainer(drcat);
            List<String> classes = new List<string>();
            if (container != null)
            {
                var currentLoaded = container.GetExports<Group3.MediaTool.Interface.IObjectRelationModel>();

                foreach( var x in currentLoaded)
                {
                    String c = x.Value.GetType().Name.ToUpper();
                    classes.Add(c.Substring(2, c.Length -2));
                }

                return classes;
            }
            return null;

        }
    }
}
